/**
 * 审批管理-审批列表控制器
 */
import $_fn from '~plugins'
import './index.less'
export default ['$scope','$rootScope','$location','$ui','http','$state',function($scope,$rootScope,$location,$ui,http,$state) {
  $scope.$_fn = $_fn
  $scope.redErr = {}
  //url参数获取
  $scope.search = $location.search()
  //页面操作项分类
  $scope.state = $scope.search.state || 1
  //警告提示
  $scope.errorToast = function(text) {
    $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
  }
  //成功提示
  $scope.successToast = function(text) {
      $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
  }
  $scope.remHight = function(){                                                 //去除报错高亮
    for(var i in $scope.redErr){
      $scope.redErr[i] = false
    }
  }


  //获取数据
  $scope.init = function(page,params){
    http.post('getAdminMessage.do',{id:$scope.search.rid},function (data) {
      if(data.errcode == 1){
        $scope.$apply(function(){
          let _data = data.data
          $scope.data = _data[0]
          
          $scope.oldPhone=$scope.data.telephone
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      }
    })
  }
  //初始化数据
  $scope.init()
  $scope.phoneOk = true
  $scope.checkPhone = function(){
    console.log(123)
    $scope.phoneOk = true
    if($scope.oldPhone!=$scope.data.telephone){
      http.post('checkPhone.do', {'telephone': $scope.data.telephone}, function (data) {
        if(data.errcode == 1){
          $scope.$apply(function(){
            $scope.successToast(data.errmsg)
          })
        }else{
          $scope.phoneOk = false
          $scope.errorToast(data.errmsg || '请求失败！')
        }
      })
    }
    
  }

  //搜索
  $scope.ok = function () {
    let url;
    let formData = {}
    if($scope.state==1){
      url = 'updateAdminMessage.do'
      if(!$scope.phoneOk){
        $scope.errorToast('手机号码已存在')
        return
      }
      if ($scope.data.username==null || $scope.data.username==undefined) {
        $scope.redErr.username = true
        $scope.errorToast('请填写用户名')
        return
      }

      formData.id = $scope.search.rid
      formData.real_name = $scope.data.real_name
      formData.telephone = $scope.data.telephone

    }else{
      url = 'updatePassword.do'
      if (!$scope.data.pwd) {
        $scope.redErr.pwd = true
        $scope.errorToast('请填写密码')
        return 
      }
      if (!$scope.data.pwd2) {
        $scope.redErr.pwd2 = true
        $scope.errorToast('请填写密码')
        return 
      }

      if ($scope.data.pwd!=$scope.data.pwd2) {
        $scope.redErr.username = true
        $scope.errorToast('两次密码不一致')
        return 
      }

      formData.id = $scope.search.rid
      formData.password = $scope.data.pwd

    }
    http.post(url, formData, function (data) {
      if(data.errcode == 1){
        $scope.$apply(function(){
          $scope.successToast(data.errmsg)
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
      }
    })
  }
}]